//
//  LLGStatuseViewModel.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/12.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit

//微博数据处理的viewModel  对应首页cell
class LLGStatuseViewModel: NSObject {

    var statuse: LLGStatuses?
    
    //转发数
    var retweetCountContent: String?
    //评论数
    var commentCountContent: String?
    //点赞数
    var unlikeCountContent: String?
    
    //转发微博内容
    var retweetContent: String?
    
    //来源
    var source: String?
    
    //会员等级
    var mbrankImage: UIImage?
    
    //认证类型等级 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
    var verifiedTypeImage: UIImage?
    
    //发微博时间显示
    var timeContent: String? {
        guard let creatDateStr = statuse?.created_at else {
            return nil
        }
        
        let creatDate = NSDate.creatDateFromString(creatDateStr)
        
        return creatDate.dateStr
        
    }
    
    init(statuse: LLGStatuses) {
        super.init()
        
        self.statuse = statuse
        
        retweetCountContent = handleCount(statuse.reposts_count, defaultTitle: "转发")
        commentCountContent = handleCount(statuse.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCount(statuse.attitudes_count, defaultTitle: "赞")
        
        handleRetweetContent(statuse)
        
        handleSource(statuse.source ?? "")
        
        handleMbrankImage(statuse.user?.mbrank ?? 0)
        
        handleVerifiedTypeImage(statuse.user?.verified_type ?? -1)
    }
    
    // MARK: --处理认证等级
    //认证类型等级 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
    private func handleVerifiedTypeImage(verifiedType: Int) {
    
        switch verifiedType {
        
        case 0:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
        
    }
    
    
    // MARK: --会员等级处理
    private func handleMbrankImage(mbrank: Int) {
    
        if mbrank >= 1 && mbrank <= 6 {
        
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
    // MARK: --来源数据处理
    private func handleSource(source: String) {
    
        if source.containsString("\">") && source.containsString("</") {
        
            let startIndex = source.rangeOfString("\">")!.endIndex
            let endIndex = source.rangeOfString("</")!.startIndex
            
            let result = source.substringWithRange(startIndex..<endIndex)
            
            self.source = "来自: " + result
        }
    }
    
    // MARK: --设置转发微博显示内容
    private func handleRetweetContent(statuse: LLGStatuses) {
    
        if statuse.retweeted_status != nil {
        
            guard let text = statuse.retweeted_status?.text, let name = statuse.retweeted_status?.user?.screen_name else {
                return
            }
            let result = "@\(name): \(text)"
            
            retweetContent = result
        }
    }
    
    // MARK: --设置显示数据字符串
    private func handleCount(count: Int, defaultTitle: String) -> String {
    
        if count > 0 {
        
            if count > 10000 {
                
                let result = CGFloat(count / 1000) / 10
                
                var resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                
                    resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return resultStr
            
            } else {
            
                return "\(count)"
            }
            
        } else {
        
            return defaultTitle
        }
    }
}
